package com.cyh.testdesignpattern.proxy.dynamic;

import android.util.Log;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyFactory {
    private Object target;

    public ProxyFactory(Object target) {
        this.target = target;
    }

    public Object getProxyInstance() {
        return Proxy.newProxyInstance(target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
                        Log.i("ProxyFactory", "jdk代理开始，代码增强。。。");
                        //调用目标对象的方法，目标函数的返回值
                        Object returnvalue = method.invoke(target, objects);
                        Log.i("ProxyFactory", "jdk代理结束，代码增强。。。");
                        return returnvalue;
                    }
                });
    }
}
